<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/style.css" />
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form action="" method="post" class="definewidth m20">
<input type="hidden" name="id" value="{$user.id}" />
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">资源名称</td>
            <td id="mName">-</td><span id="mNameMsg"></span>
        </tr>
        <tr>
            <td class="tableleft">url</td>
            <td><input id="mUrl" type="text" name="url"/><span id="mUrlMsg"></span></td>
        </tr>
        <tr>
            <td class="tableleft">状态</td>
            <td>
                <input type="radio" name="status" value="0"/> 有效
                <input type="radio" name="status" value="1" /> 被禁用
                <input type="radio" name="status" value="3" /> 未启用
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button id="update" type="button" class="btn btn-primary" type="button">更新</button>&nbsp;&nbsp;<button type="button" class="btn btn-success" name="backid" onclick="back()" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>

    function validatamUrl() {
        var name = $("#mUrl").val();
        var msg = $("span[id='mUrlMsg']");
        if(name == "") {
            msg.html("url不能为空").css("color", "red");
            return false;
        } else {
            msg.html("ok").css("color", "green");
            return true;
        }
    }

    function validataState() {
        var all = $("input[name='status']");
        for(var i = 0; i < all.length; i++) {
            if($(all[i]).prop("checked")) {
                return true;
            }
        }
        alert("状态为必选项");
        return false;
    }

    $(function() {

        $("#mUrl").blur(function() {
            validatamUrl();
        });
    });

    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    let mId=null;

    function back(){
        window.location.href="/pages/resource/index.html";
    }

    $(function () {

		    let url=window.location.toString();
		    mId=url.GetValue("mId");
		    $.getJSON("/resource/edit.do",{mId:mId},function (vo){
		        let obj=vo.obj;
		        let mName=obj.mName;
		        let mUrl=obj.mUrl;
		        let mState=obj.mState;
		        $("#mName").html(mName);
                $("#mUrl").val(mUrl);
                if (mState==0){
                    $("input[name='status']:eq(0)").prop("checked",true);
                }else if (mState==1){
                    $("input[name='status']:eq(1)").prop("checked",true);
                }else {
                    $("input[name='status']:eq(2)").prop("checked",true);
                }
            });

    });
    $("#update").click(function (){
        if (validatamUrl()&&validataState()){
            let mName=$("#mName").html();
            let mUrl=$("#mUrl").val();
            let mState=$("input[name='status']:checked").val();
            let menu={mId:mId,mName:mName,mUrl:mUrl,mState:mState};
            $.getJSON("/resource/update.do",menu,function (vo){
                if(vo.code==200) {
                    window.location.href = "/pages/resource/index.html";
                }else{
                    alert("更新失败！"+vo.msg);
                }
            });
        }
    });
</script>